﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace WEALTH.Manager.BaseSystem
{
    public partial class RoleList : BasePage
    {
        ExcuteDao menu = new ExcuteDao();
        string strMsg = "", GroupCode = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                btnDelete.OnClientClick = " return confirm('确认删除所选角色吗？');";
                ShowTree();

                if (Session["msg"] != null)
                {
                    ShowMsg(Session["msg"].ToString(), 0);
                    Session["msg"] = null;
                }

            }
        }



        //显示树
        public void ShowTree()
        {
            TreeView_Group.Nodes.Clear();
            TreeNode node_0 = new TreeNode();
            node_0.ImageUrl = "../Images/Small/calendar.gif";
            node_0.Text = "网站后台管理系统 <a herf='#'></a>";
            node_0.Value = "1";
            node_0.Text += "&nbsp;&nbsp;<a target='_self' href='RoleInsert.aspx?code=" + node_0.Value.ToString().Trim() + "'><font color='#0000FF'>增加角色</font></a>";

            node_0.SelectAction = TreeNodeSelectAction.Expand;
            node_0.Expanded = true;
            this.TreeView_Group.Nodes.Add(node_0);

            this.ViewState["ds"] = menu.GetQueryStrGroupMenu();

            AddTree(node_0.Value, node_0);

        }

        //添加子节点
        public void AddTree(String ParentID, TreeNode pNode)
        {
            DataSet ds = (DataSet)this.ViewState["ds"];
            DataView dvTree = new DataView(ds.Tables[0]);
            dvTree.RowFilter = " Role_Pcode = '" + ParentID + "'";
            foreach (DataRowView Row in dvTree)
            {
                TreeNode Node = new TreeNode();
                //添加当前节点的子节点
                Node.Value = Row["Role_Code"].ToString().Trim();
                Node.Text = Row["Role_Name"].ToString().Trim() + "<a herf='#'></a>";
                Node.Text += " <font style='font-size: 11px' color='#808080'>[" + Node.Value.ToString().Trim() + "]</font> ";

                if (Row["Role_IsUse"].ToString().Trim() != "1")
                {
                    Node.Text += " <font color='#ff0000'>[停用]</font> ";
                }
                Node.ImageUrl = "../Images/Small/group.gif";

                //Node.Text += "&nbsp;&nbsp;<a target='_self' href='GroupInsert.aspx?code=" + publ.GetUrlToSend(Node.Value.ToString().Trim()) + "'><font color='#0000FF'>增加子角色</font></a>";
                Node.Text += "&nbsp;&nbsp;<a target='_self' href='RoleUpdate.aspx?code=" + Node.Value.ToString().Trim() + "'><font color='#0000FF'>编辑</font></a>";
                Node.Text += "&nbsp;&nbsp;<a target='_self' href='GroupMenus.aspx?code=" + Node.Value.ToString().Trim() + "'><font color='#0000FF'>菜单</font></a>";
                pNode.ChildNodes.Add(Node);
                Node.Expanded = true;


                if (pNode != null)
                {
                    GroupCode = "'" + Row["Role_Code"].ToString().Trim() + "'";

                }

                AddTree(Row["Role_Code"].ToString(), Node);    //再次递归

            }
        }

        //删除节点
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            string MenuCodeAll = GetTreeCheck(TreeView_Group.Nodes[0]);
            string[] values = MenuCodeAll.Split(',');

            if (MenuCodeAll == "")
            {
                //Response.Write("<script>alert('未选择角色，删除失败！');</script>");
                ShowMsg("未选择角色，删除失败！", 1);
                return;
            }
            else
            {
                for (int i = 0; i < values.Length; i++)
                {
                    menu.DelGroup(values[i].ToString().Trim());
                }
            }

            ShowTree();
            //Session["Msg"] = "<script>alert('操作成功！(某些角色若被用户使用,则无法删除!)');</script>";
            ShowMsg("操作成功！(某些角色若被用户使用,则无法删除!", 0);
            return;
        }

        /* 得到所选择的节点
         * 调用方法 string MenuCodeAll = GetTreeCheck(TreeView_Group.Nodes[0]);
         * 
         */
        string FunctionString = "";
        public string GetTreeCheck(TreeNode tn)
        {
            foreach (TreeNode treenode in tn.ChildNodes)
            {
                if (treenode.Checked)
                {
                    string va = treenode.Value;
                    string vb = treenode.Text;
                    FunctionString += ", " + va + "";
                }
                else
                {
                }

                GetTreeCheck(treenode);//调用自身函数循环遍历TreeView

            }
            return FunctionString;//返回FunctionString字符串
        }
    }
}